<template>
  <nut-collapse v-model="activeName" :accordion="true">
    <nut-collapse-item v-for="item in dataList" :key="item.ID" :title="item.UserName" :value="item.applyDate" :name="item.ID">
      <template #extra>{{ item.leaveNameFull }}</template>
     <view style="margin-bottom: 10px;">  开始时间: {{ item.dateStart }}</view>
  <view style="margin-bottom: 10px;">  结束时间: {{ item.dateEnd }} </view>
  <view style="margin-bottom: 10px;"> 请假/加班天数: {{ item.dateCount }}</view>
  <view style="margin-bottom: 10px;">请假原因: {{ item.leaveCause }}</view>
    

      <nut-button  color="#7ba3f1" size="small" @click="approve(item.ID)">通过</nut-button>
      <nut-button color="#ce723c" size="small" @click="reject(item)">驳回</nut-button>
    </nut-collapse-item>
  </nut-collapse>
</template>
<script>
import { reactive, ref, toRefs } from "vue";
import Taro from "@tarojs/taro";
export default {
  setup() {
    const userData =  Taro.getStorageSync("userData");
    const userid =  userData.id;
    console.log("当前登录的用户id"+userData.id);
    console.log(userData.depaCode);
    const activeName = ref(1);
    const dataList = ref([]);

    const select = async () => {
      const formData = {
        view: "V_GetLeaveCheck",
        userId: "string",
        depaCode: userData.depaCode,
      };
      try {
        const response = await new Promise((resolve, reject) => {
          Taro.request({
            url: "https://sunyukq.transpheretms.com:8078/api/Select",
            method: "POST",
            data: formData,
            header: {
              "Content-Type": "application/json"
            },
            success: function (res) {
              if (res && res.statusCode === 200) {
                console.log("查询到数据");
                resolve(res.data);
              } else {
                console.log("查询失败");
                reject(new Error("查询失败"));
              }
            }
          })
        });
        dataList.value = response;
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    };

    const approve = async (id) => {
      console.log(`Approving ID: ${id}`);
      try {
    const response = await new Promise((resolve, reject) => {
      Taro.request({
        url: "https://sunyukq.transpheretms.com:8078/api/leaveCheckPass",
        method: "POST",
        data: {
          userid: userid, // 当前登录用户的ID
          ID: id // 请假记录的ID
        },
        header: {
          "Content-Type": "application/json"
        },
        success: function(res) {
          if (res && res.statusCode === 200) {
            resolve(res.data);
            select();  // 刷新数据
          } else {
            reject(new Error("Error approving"));
          }
        },
        fail: function(err) {
          reject(err);
        }
      });
    });
    console.log("Approve response:", response); 
  } catch (error) {
    console.error("Error approving:", error);
  }
    };

    const reject =async  (item) => {
      console.log(`Rejecting ID: ${item.id}`);
      let a = "0";  // 默认值
  if (item.leaveNameFull === "年假申请单") {
    a = "1";
  } else if (item.leaveNameFull === "全薪病假申请单") {
    a = "2";
  }

  try {
    const response = await new Promise((resolve, reject) => {
      Taro.request({
        url: "https://sunyukq.transpheretms.com:8078/api/leaveCheckReturn",
        method: "POST",
        data: {
          ID: item.ID,
          a: a
        },
        header: {
          "Content-Type": "application/json"
        },
        success: function(res) {
          if (res && res.statusCode === 200) {
            resolve(res.data);
            select();  // 刷新数据
          } else {
            reject(new Error("Error rejecting"));
          }
        },
        fail: function(err) {
          reject(err);
        }
      });
    });
    console.log("Reject response:", response); 
  } catch (error) {
    console.error("Error rejecting:", error);
  }
    };

    // 这里你可能想要在组件挂载后立即执行select函数
    // 或者在其他合适的生命周期内执行
    select();

    return {
      activeName,
      dataList,
      approve,
      reject
    };
  },
};
</script>
